﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MoonLight.LibCommon
{
    public class CalMD5
    {
        public static string GetMD5(string file_name)
        {
            string result = "";

            try
            {
                FileStream file = new FileStream(file_name, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] ret_val = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < ret_val.Length; i++)
                {
                    sb.Append(ret_val[i].ToString("x2"));
                }
                result = sb.ToString();
            }
            catch (Exception ex)
            {
                Logger.WriteLog("get md5 of [" + file_name + "] fail. [" + ex.Message + "]");
            }

            return result;
        }
    }
}
